package com.wang.controller;


import com.wang.base.http.RestResponse;
import com.wang.domain.Medicine;
import com.wang.domain.SmallCategory;
import com.wang.model.Medicine.*;
import com.wang.service.LargeCategoryService;
import com.wang.service.MedicineService;
import com.wang.service.SmallCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WangZai
 * @since 2023-05-06
 */
@CrossOrigin
@RestController
@RequestMapping("/td/api/medicine")
@Api(tags = "药品模块接口说明")
public class MedicineController {
    private final ModelMapper modelMapper;
    private final MedicineService medicineService;
    private final LargeCategoryService largeCategoryService;
    private final SmallCategoryService smallCategoryService;

    @Autowired
    public MedicineController(ModelMapper modelMapper, MedicineService medicineService, LargeCategoryService largeCategoryService, SmallCategoryService smallCategoryService) {
        this.modelMapper = modelMapper;
        this.medicineService = medicineService;
        this.largeCategoryService = largeCategoryService;
        this.smallCategoryService = smallCategoryService;
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/adminSelectMedicineData")
    @ApiOperation("管理员获取药品数据接口说明")
    public RestResponse<Object> adminSelectMedicineData(@RequestBody MedicineSelectVM model) {
        List<Medicine> medicineList = medicineService.conditionSelect(model);
        if (medicineList.isEmpty()) {
            return RestResponse.failure("无满足条件的药品");
        } else {
            List<MedicineResponseVM> medicineResponseVMList = new ArrayList<>();
            for(Medicine medicine:medicineList){
                MedicineResponseVM medicineResponseVM = modelMapper.map(medicine,MedicineResponseVM.class);
                medicineResponseVM.setLargeCategoryName(largeCategoryService.getLargeCategoryById(medicine.getLargeCategoryId()).getLargeCategoryName());
                medicineResponseVM.setSmallCategoryName(smallCategoryService.getSmallCategoryById(medicine.getSmallCategoryId()).getSmallCategoryName());
                medicineResponseVMList.add(medicineResponseVM);
            }
            return RestResponse.ok(medicineResponseVMList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/addData")
    @ApiOperation("管理员添加药品数据接口说明")
    public RestResponse<Object> addData(@RequestBody AddMedicineVM model) {
        Medicine medicine = modelMapper.map(model, Medicine.class);
        if (medicineService.getMedicineByName(model.getMedicineName()) == null) {
            medicineService.addMedicine(medicine);
            return RestResponse.ok("添加成功！");
        } else {
            return RestResponse.failure("信息重复，添加失败！！！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/deleteData/{medicineId}")
    @ApiOperation("管理员删除药品数据接口说明")
    public RestResponse<Object> deleteData(@PathVariable Integer medicineId) {
        if (medicineService.removeById(medicineId)) {
            return RestResponse.ok("删除成功！");
        } else {
            return RestResponse.failure("删除失败！！！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/selectMedicineData")
    @ApiOperation("用户获取药品数据接口说明")
    public RestResponse<Object> userSelectMedicineData(@RequestParam("medicineName") String medicineName) {
        List<Medicine> medicineLists = medicineService.getList();
        List<Integer> largeIds = new ArrayList<>();
        List<Integer> smallIds = new ArrayList<>();
        if (!medicineLists.isEmpty()) {
            for (Medicine medicine : medicineLists) {
                if (!largeIds.contains(medicine.getLargeCategoryId())) {
                    largeIds.add(medicine.getLargeCategoryId());
                }
                if (!smallIds.contains(medicine.getSmallCategoryId())) {
                    smallIds.add(medicine.getSmallCategoryId());
                }
            }
        } else {
            return RestResponse.failure("无相关数据！！");
        }

        List<LargeCategoryResponseVM> largeList = new ArrayList<>();
        for (Integer largeId : largeIds) {
            LargeCategoryResponseVM largeModel = new LargeCategoryResponseVM();
            List<SmallCategoryResponseVM> smallList = new ArrayList<>();
            for (Integer smallId : smallIds) {
                SmallCategory smallCategory = smallCategoryService.getSmallCategoryById(smallId);
                if (smallCategory.getLargeCategoryId().equals(largeId)) {
                    SmallCategoryResponseVM smallModel = new SmallCategoryResponseVM();
                    smallModel.setSmallCategoryName(smallCategory.getSmallCategoryName());
                    if (medicineName.equals("wz")) {
                        smallModel.setMedicineList(medicineService.getListBySmallCategoryId(smallId));
                    } else {
                        smallModel.setMedicineList(medicineService.getListBySmallCategoryIdAndDimName(smallId, medicineName));
                    }
                    smallList.add(smallModel);
                }
            }
            largeModel.setMedicineList(smallList);
            largeModel.setLargeCategoryName(largeCategoryService.getLargeCategoryById(largeId).getLargeCategoryName());
            largeList.add(largeModel);
        }
        if (largeList.isEmpty()) {
            return RestResponse.failure("无相关数据");
        }
        return RestResponse.ok(largeList);
    }


    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/updateData")
    @ApiOperation("管理员修改药品数据接口说明")
    public RestResponse<Object> updateData(@RequestBody UpdateMedicineVM model) {
        Medicine medicine = medicineService.getMedicineById(model.getMedicineId());
        Medicine medicine1 = medicineService.getMedicineByName(model.getMedicineName());
        if (medicine1 != null && !medicine1.getMedicineId().equals(model.getMedicineId())) {
            return RestResponse.failure("药品名重复，修改失败！！！");
        }

        medicine.setMedicineName(model.getMedicineName());
        medicine.setLargeCategoryId(model.getLargeCategoryId());
        medicine.setSmallCategoryId(model.getSmallCategoryId());
        medicine.setPharmacology(model.getPharmacology());
        medicine.setIndications(model.getIndications());
        medicine.setUsage(model.getUsage());
        medicine.setNote(model.getNote());
        medicineService.updateMedicine(medicine);
        return RestResponse.ok("修改成功！");


    }
}
